package com.dw.love.dwade.view;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dw.love.dwade.R;
import com.dw.love.dwade.utils.StatusBarUtil;
import com.dw.love.dwade.view.fragment.FragmentHomeNews;
import com.dw.love.dwade.view.fragment.FragmentHomePic;
import com.dw.love.dwade.view.fragment.FragmentHomeVideo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.common_title_tv)
    TextView commonTitleTv;
    @BindView(R.id.common_toolbar)
    Toolbar commonToolbar;
    @BindView(R.id.bottom_navigation_view)
    BottomNavigationView mBottomNavigationView;
    @BindView(R.id.navi_home_left)
    NavigationView mNaviHomeLeft;
    @BindView(R.id.common_drawer_layout)
    DrawerLayout mCommonDrawerLayout;
    @BindView(R.id.framelayout_main_group)
    FrameLayout framelayoutMainGroup;
    private ActionBarDrawerToggle mToggle;
    private List<Fragment> mFragmentList;
    private int mLastFragmentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initToolBar();
        initBottomNavigationView();
        framelayoutMainGroup.getBackground().setAlpha(120);
        initFragment();
    }

    private void initFragment() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new FragmentHomeNews());
        mFragmentList.add(new FragmentHomeVideo());
        mFragmentList.add(new FragmentHomePic());
    }


    private void initBottomNavigationView() {
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.tab_main_pager:
                        selectFragment(0);
                        break;
                    case R.id.tab_main_video:
                        selectFragment(1);
                        break;
                    case R.id.tab_main_picture:
                        selectFragment(2);
                        break;
                }
                return true;
            }
        });
    }

    private void selectFragment(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = mFragmentList.get(position);
        Fragment lastFragment = mFragmentList.get(mLastFragmentIndex);
        mLastFragmentIndex = position;
        ft.hide(lastFragment);
        if (!currentFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.framelayout_main_group,currentFragment).commit();
        }
        ft.show(currentFragment);
        ft.commitAllowingStateLoss();// 与commit区别在于 能在activity保存状态后保存
    }

    private void initToolBar() {
        setSupportActionBar(commonToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        commonTitleTv.setText("Heat Home");
        StatusBarUtil.setStatusColor(getWindow(), ContextCompat.getColor(this, R.color.main_status_bar_blue), 1f);
        //toolbar 与 drawerlayout 关联
        mToggle = new ActionBarDrawerToggle(this, mCommonDrawerLayout, commonToolbar, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //获取mDrawerLayout中的第一个子布局，也就是布局中的RelativeLayout
                //获取抽屉的view
                View mContent = mCommonDrawerLayout.getChildAt(0);
                float scale = 1 - slideOffset;
                float endScale = 0.8f + scale * 0.2f;
                float startScale = 1 - 0.3f * scale;

                //设置左边菜单滑动后的占据屏幕大小
                drawerView.setScaleX(startScale);
                drawerView.setScaleY(startScale);
                //设置菜单透明度
                drawerView.setAlpha(0.6f + 0.4f * (1 - scale));

                //设置内容界面水平和垂直方向偏转量
                //在滑动时内容界面的宽度为 屏幕宽度减去菜单界面所占宽度
                mContent.setTranslationX(drawerView.getMeasuredWidth() * (1 - scale));
                //设置内容界面操作无效（比如有button就会点击无效）
                mContent.invalidate();
                //设置右边菜单滑动后的占据屏幕大小
                mContent.setScaleX(endScale);
                mContent.setScaleY(endScale);
            }
        };
        mToggle.syncState();//关键
        mCommonDrawerLayout.addDrawerListener(mToggle);
    }
}
